import { createRouter, createWebHashHistory } from 'vue-router'
import NProgress from 'nprogress'

const routes = [
  {
    path: '/',
    name: '登录',
    component: () => import('@/views/login'),
  },
  {
    path: '/registered',
    name: '注册',
    component: () => import('@/views/registered'),
  },
  {
    path: '/forgetPass',
    name: '忘记密码',
    component: () => import('@/views/forgetPass'),
  },
  {
    path: '/changePass',
    name: '修改手机号',
    component: () => import('@/views/changePass'),
  },
  {
    path: '/editPass',
    name: '修改密码',
    component: () => import('@/views/editPass'),
  },
  {
    path: '/home',
    name: '主页',
    component: () => import('@/views/home'),
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        name: '欢迎',
        component: () => import('@/views/welcome'),
      },
      {
        path: '/userManage',
        name: '员工管理',
        redirect: '/userList',
        children: [
          {
            path: '/userList',
            name: '员工列表',
            component: () => import('@/views/userManage/list'),
            children: [],
          },
          {
            path: '/userRole',
            name: '角色权限',
            component: () => import('@/views/userManage/role'),
            children: [],
          },
        ],
      },
      {
        path: '/shopManage',
        name: '店铺管理',
        component: () => import('@/views/shopManage'),
        redirect: '/shopManage/level',
        children: [
          {
            path: '/shopManage/level',
            name: '店铺等级',
            component: () => import('@/views/shopManage/level'),
          },
          {
            path: '/shopManage/logoff',
            name: '店铺注销',
            component: () => import('@/views/shopManage/logoff'),
          },
          {
            path: '/shopManage/dails',
            name: '代理商',
            component: () => import('@/views/shopManage/dails'),
            children: [
              {
                path: '/shopManage/dails',
                name: '代理商列表',
                component: () => import('@/views/shopManage/dails'),
              },
              {
                path: '/shopManage/dails/detail',
                name: '代理商详情',
                component: () => import('@/views/shopManage/dails/detail'),
                meta: {
                  openName: "/shopManage/dails",
                },
              }
            ],
          },
        ],
      },
      {
        path: '/goodsManage',
        name: '商品管理',
        component: () => import('@/views/goodsManage'),
        redirect: '/goodsManage/mealGoods',
        children: [
          {
            path: '/goodsManage/mealGoods',
            name: '套餐商品',
            component: () => import('@/views/goodsManage/mealGoods'),
            children: [
              {
                path: '/goodsManage/mealGoods/add',
                name: '套餐商品新增',
                component: () => import('@/views/goodsManage/mealGoods/add'),
                meta: {
                  openName: '/goodsManage/mealGoods',
                },
              },
              {
                path: '/goodsManage/mealGoods/detail',
                name: '套餐商品详情',
                component: () => import('@/views/goodsManage/mealGoods/detail'),
                meta: {
                  openName: '/goodsManage/mealGoods',
                },
              },
            ]
          },
          {
            path: '/goodsManage/cardGoods',
            name: '号卡商品',
            component: () => import('@/views/goodsManage/cardGoods'),
            redirect: '/goodsManage/cardGoods/list',
            children: [
              {
                path: '/goodsManage/cardGoods/list',
                name: '商品列表',
                component: () => import('@/views/goodsManage/cardGoods/list'),
                meta: {
                  openName: "/goodsManage/cardGoods",
                },
              },
              {
                path: '/goodsManage/cardGoods/editGoods',
                name: '新增/编辑商品',
                component: () => import('@/views/goodsManage/cardGoods/editGoods'),
                meta: {
                  openName: "/goodsManage/cardGoods",
                },
              },
              {
                path: '/goodsManage/cardGoods/goodsDetail',
                name: '商品详情',
                component: () => import('@/views/goodsManage/cardGoods/goodsDetail'),
                meta: {
                  openName: "/goodsManage/cardGoods",
                },
              },
              {
                path: '/goodsManage/cardGoods/goodsZiliao',
                name: '商品资料',
                component: () => import('@/views/goodsManage/cardGoods/goodsZiliao'),
                meta: {
                  openName: "/goodsManage/cardGoods",
                },
              },
            ],
          },
        ]
      },
      {
        path: '/modelManage',
        name: '型号管理',
        component: () => import('@/views/modelManage'),
      },
      {
        path: '/equipment',
        name: '设备商品',
        component: () => import('@/views/equipment'),
        children: [
          {
            path: '/equipment/add',
            name: '设备新增',
            component: () => import('@/views/equipment/add'),
            meta: {
              openName: "/equipment",
            },
          },
          {
            path: '/equipment/detail',
            name: '设备商品详情',
            component: () => import('@/views/equipment/detail'),
            meta: {
              openName: "/equipment",
            },
          },
        ]
      },
      {
        path: '/inventory',
        name: '库存管理',
        component: () => import('@/views/inventory'),
        redirect: '/inventoryGoods',
        children: [
          {
            path: '/inventoryGoods',
            name: '库存',
            component: () => import('@/views/inventory/goods'),
            children: [
              {
                path: '/inventoryGoodsDetail',
                name: '库存详情',
                component: () => import('@/views/inventory/goods/detail'),
                meta: {
                  openName: "/inventoryGoods",
                },
              }
            ],
          },
          {
            path: '/inventoryGoodsAll',
            name: '平台库存',
            component: () => import('@/views/inventory/goodsAll'),
            children: [],
          },
          {
            path: '/inventoryAgents',
            name: '代理商库存',
            component: () => import('@/views/inventory/agents'),
            children: [
              {
                path: '/inventoryAgentsDetail',
                name: '代理商库存详情',
                component: () => import('@/views/inventory/agents/detail'),
                meta: {
                  openName: "/inventoryAgents",
                },
              }
            ],
          },
          {
            path: '/inventoryRecord',
            name: '库存操作记录',
            component: () => import('@/views/inventory/record'),
            children: [],
          },
          {
            path: '/inventoryTransfer',
            name: '调拨单',
            component: () => import('@/views/inventory/transfer'),
            children: [],
          },
          {
            path: '/inventoryErrList',
            name: '异常同步记录',
            component: () => import('@/views/inventory/errList'),
            children: [],
          },
        ],
      },
      {
        path: '/activeManage',
        name: '激活管理',
        component: () => import('@/views/activeManage'),
        children: [
          {
            path: '/activeManage/list',
            name: '激活管理',
            component: () => import('@/views/activeManage/list'),
          },
          {
            path: '/activeManage/record',
            name: '激活记录',
            component: () => import('@/views/activeManage/record'),
          },
          {
            path: '/activeManage/revote',
            name: '激活核减记录',
            component: () => import('@/views/activeManage/revote'),
          },
        ]
      },
      {
        path: '/chargeManage',
        name: '充值管理',
        component: () => import('@/views/chargeManage'),
        children: [
          {
            path: '/chargeManage/list',
            name: '充值管理',
            component: () => import('@/views/chargeManage/list'),
          },
          {
            path: '/chargeManage/record',
            name: '充值记录',
            component: () => import('@/views/chargeManage/record'),
          },
          {
            path: '/chargeManage/revote',
            name: '充值核减记录',
            component: () => import('@/views/chargeManage/revote'),
          },
        ]
      },
      {
        path: '/statistics',
        name: '统计报表',
        component: () => import('@/views/statistics'),
      },
      {
        path: '/orderMng',
        name: '订单管理',
        redirect: '/orderMng/goodsOrder',
        children: [
          {
            path: '/orderMng/goodsOrder',
            name: '商品订单',
            component: () => import("@/views/orderMng/goodsOrder"),
          },
          {
            path: '/orderMng/goodsOrder/detail',
            name: '商品订单详情',
            component: () => import("@/views/orderMng/goodsOrder/detail"),
            meta: {
              openName: "/orderMng/goodsOrder",
            },
          },
          {
            path: '/orderMng/sellCardsOrder',
            name: '售卡订单',
            component: () => import('@/views/orderMng/sellCardsOrder'),
            children: [
              {
                path: '/orderMng/sellCardsOrder/orderDetail',
                name: '售卡订单详情',
                component: () => import('@/views/orderMng/sellCardsOrder/orderDetail'),
                meta: {
                  openName: '/orderMng/sellCardsOrder',
                },
              }
            ]
          },
          {
            path: '/orderMng/commissionOrder',
            name: '佣金订单',
            component: () => import("@/views/orderMng/commissionOrder"),
          },
          {
            path: '/orderMng/cashOrder',
            name: '提现订单',
            component: () => import("@/views/orderMng/cashOrder"),
            children: [
              {
                path: '/orderMng/cashOrder/orderDetail',
                name: '提现订单详情',
                component: () => import('@/views/orderMng/cashOrder/orderDetail'),
                meta: {
                  openName: '/orderMng/cashOrder',
                },
              }
            ]
          },
        ]
      },
      {
        path: '/softwareRelease',
        name: '版本管理',
        component: () => import("@/views/softwareRelease"),
        redirect: '/softwareRelease/projectManagement',
        children: [
          {
            path: '/softwareRelease/projectManagement',
            name: '项目管理',
            component: () => import("@/views/softwareRelease/projectManagement"),
          },
          {
            path: '/softwareRelease/products',
            name: '套餐产品列表',
            component: () => import("@/views/softwareRelease/products"),
          },
          {
            path: '/softwareRelease/softwareManagement',
            name: '软件管理',
            component: () => import("@/views/softwareRelease/softwareManagement"),
          },
        ]
      },
      {
        path: '/sysManage',
        name: '系统管理',
        children: [
          {
            path: '/sysManage/dividendConfig',
            name: '分红配置',
            component: () => import('@/views/sysManage/dividendConfig'),
          },
          {
            path: '/sysManage/withdrawConfig',
            name: '提现配置',
            component: () => import('@/views/sysManage/withdrawConfig'),
          },
          {
            path: '/sysManage/sellCardConfig',
            name: '售卡佣金',
            component: () => import('@/views/sysManage/sellCardConfig'),
          },
          {
            path: '/sysManage/monthTask',
            name: '月结任务',
            component: () => import('@/views/sysManage/monthTask'),
          },
          {
            path: '/sysManage/sysMsg',
            name: '系统消息',
            component: () => import('@/views/sysManage/sysMsg'),
          },
          {
            path: '/bannerMng',
            name: '轮播图管理',
            component: () => import('@/views/bannerMng'),
          },
          {
            path: '/sysManage/policy',
            name: '服务政策',
            component: () => import('@/views/sysManage/policy'),
          },
        ]
      },
      {
        path: '/posterManage',
        name: '海报管理',
        children: [
          {
            path: '/posterManage/posterConfig',
            name: '海报配置',
            component: () => import('@/views/posterManage/posterConfig'),
          },
          {
            path: '/posterManage/list',
            name: '海报列表',
            component: () => import('@/views/posterManage/list'),
          },
        ]
      },
      {
        path: '/accountMng',
        name: '账户管理',
        component: () => import('@/views/accountMng'),
        children: [
          {
            path: '/commissionMng',
            name: '佣金管理',
            component: () => import('@/views/accountMng/commissionMng'),
          },
          {
            path: '/sellCardsMng',
            name: '售卡管理',
            component: () => import('@/views/accountMng/sellCardsMng'),
            children: [
              {
                path: '/sellCardsMng/detail',
                name: '售卡文件详情',
                component: () => import('@/views/accountMng/sellCardsMng/detail'),
                meta: {
                  openName: '/sellCardsMng',
                },
              }
            ]
          },
        ]
      },
    ],
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

const wtiterPath = ['/', '/registered', '/forgetPass',]

router.beforeEach((to, from, next) => {
  NProgress.start()
  let token = window.localStorage.getItem('USER_TOKEN_INRE')
  if (!token && !wtiterPath.includes(to.path)) {
    next({ path: '/' })
    return
  }

  next()
})

router.afterEach(() => {
  NProgress.done()
})

export default router
